package com.nbstar.film.spider.spider;

import com.nbstar.film.spider.pojo.FilmInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URLEncoder;

@Slf4j
public class VqqSpider extends BaseSpider {
    @Override
    public String getContentId(String filmName) throws Exception {
        String  url = "https://v.qq.com/x/search/?q=" + URLEncoder.encode(filmName);
        FilmInfo filmInfo = new FilmInfo();
        String html = firefoxDriverRequest.getHtml(url);
        //System.out.println(html);
        Document doc = Jsoup.parse(html);
        Elements infos = doc.getElementsByClass("_infos");
        for (Element info : infos) {
            Elements result_title = info.getElementsByClass("result_title");
            if (result_title.size() > 0) {

                Elements links = result_title.select("a[href]");
                return links.get(0).attr("href");
            }
        }

        return URLEncoder.encode(filmName);
    }

    @Override
    public FilmInfo getFilmInfo(String url) throws Exception {
        String html = getRequest().getHtml(url);
        Document doc = Jsoup.parse(html);

        Elements title_en = doc.getElementsByClass("title_en");
        FilmInfo filmInfo = new FilmInfo();
        if (title_en.size() > 0) {
            filmInfo.setEname(title_en.text());
        }

        Elements score_v = doc.getElementsByClass("video_score");
        if (score_v.size() > 0) {
            //Elements score = score_v.get(0).getElementsByClass("score");

            try {
                filmInfo.setAverage(Double.parseDouble(score_v.text().replaceAll("分", "")));
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }

        Elements video_tags = doc.getElementsByClass("video_tags _video_tags");

        if (video_tags != null) {
            String tagsArray[] = video_tags.text().split(" ");
            int i = 0;
            for (String tag : tagsArray) {
                if (i == 0) {
                    filmInfo.setArriveAddress(tag.trim());
                }

                if (i == 1) {
                    filmInfo.setArriveDate(tag.trim());
                }

                if (i == 2) {
                    filmInfo.setCategory(tag.trim());
                }

                if (i == 3) {
                    filmInfo.setOnlinePlatform(tag.trim());
                }

                i++;
            }
        }

        Elements director = doc.getElementsByClass("director");

        String last = "";
        if (director != null && !StringUtils.isEmpty(director.text())) {
            String dirs[] = director.text().split(" ");
            for(String d : dirs) {

                if (last.contains("导演")) {
                    filmInfo.setDirector(d.trim().replace("导演:", ""));
                    last = "";
                }


                if (last.contains("演员")) {
                    filmInfo.setActor(d.trim().replace("演员:", ""));
                    last = "";
                }

                if (d.trim().contains("导演")) {
                    last = d.trim();
                }
                if (d.trim().contains("演员")) {
                    last = d.trim();
                }
            }
        }

        Elements summary = doc.getElementsByClass("summary");
        if (summary != null) {
            filmInfo.setDesc(summary.text());
        }

        Elements video_type_cf = doc.getElementsByClass("video_info");
        if (video_type_cf.size() > 0) {
            Elements type_item = video_type_cf.get(0).getElementsByClass("type_item");
            for (Element item : type_item) {
                Element tit = item.getElementsByClass("type_tit").first();
                Element txt =  item.getElementsByClass("type_txt").first();
                if (tit.text().equals("别　名:")) {
                    filmInfo.setAliasName(txt.text().trim());
                }

                if (tit.text().trim().equals("地　区:")) {
                    filmInfo.setArriveAddress(txt.text());
                }

                if (tit.text().trim().equals("语　言:")) {

                }


            }
        }

        Element video_type_even = doc.getElementsByClass("video_type video_type_even cf").first();
        if (video_type_even != null) {
            Elements type_item = video_type_even.getElementsByClass("type_item");
            for (Element item : type_item) {
                Element tit = item.getElementsByClass("type_tit").first();
                Element txt = item.getElementsByClass("type_txt").first();
                if (tit.text().equals("总集数:")) {
                    filmInfo.setPartNum(txt.text().trim());
                }

                if (tit.text().equals("出品时间:")) {
                    filmInfo.setArriveDate(txt.text().trim());
                }
            }
        }

        Element tag_list = doc.getElementsByClass("tag_list").first();
        if (tag_list != null) {
            filmInfo.setCategory(tag_list.text());
        }

        Element desc_txt = doc.getElementsByClass("desc_txt").first();
        if (desc_txt != null) {
            filmInfo.setDesc(desc_txt.text());
        }

        //Element mark_v =  doc.getElementById("_vip_player_sec");
        Elements mark_v =  doc.getElementsByClass("mod_figure mod_figure_list mod_figure_list_sm").first().getElementsByClass("mark_v");
        if (mark_v != null && mark_v.select("img") != null) {
            filmInfo.setNeedVip(true);
        }



        filmInfo.setSuccess(true);
        return filmInfo;
    }
}
